//
//  IconDetailsViewController.m
//  InSuger
//
//  Created by dllo on 15/11/27.
//  Copyright © 2015年 Dior_3g. All rights reserved.
//

#import "IconDetailsViewController.h"
#import "IconDetailsTableViewCell.h"
#import "Header.h"
#import "HomePageTableViewModel.h"
#import "HomePageDetailViewController.h"
@interface IconDetailsViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong) NSMutableArray *topicArr;
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) MBProgressHUD *progress;
@end

@implementation IconDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.progress = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    _progress.labelText = @"Loading...";
    _progress.mode = MBProgressHUDModeDeterminate;
    
    [self createTableView];
    
    [self createData];
    
}
#pragma mark create tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.904048 * self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    self.tableView.rowHeight = 150;
    self.tableView.separatorStyle = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).with.offset(0);
        make.right.equalTo(self.view.mas_right).with.offset(0);
        make.top.equalTo(self.view.mas_top).with.offset(0);
        make.height.equalTo(self.view);
    }];
    
    @weakify(self);
    [self addColorChangedBlock:^{
        @strongify(self);
        self.navigationController.navigationBar.nightBackgroundColor = UIColorFromRGB(0x343434);
        self.navigationController.navigationBar.normalBackgroundColor = [UIColor redColor];
        self.tableView.nightBackgroundColor = UIColorFromRGB(0x343434);
    }];
  
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
}

#pragma mark TableView Delegate Method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.topicArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    IconDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[IconDetailsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
    }
    cell.model = self.topicArr[indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomePageDetailViewController * homePageDetailsVC = [[HomePageDetailViewController alloc] init];
    homePageDetailsVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:homePageDetailsVC animated:YES];
    
    homePageDetailsVC.model = self.topicArr[indexPath.row];
}
#pragma mark Data
- (void)createData
{
    NSString *url = [NSString stringWithFormat:@"http://open3.bantangapp.com/topic/list?app_installtime=1445332782.662591&app_versions=4.3&category=%@&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&oauth_token=bf8be334eb1fd06b1bf65f5883dd415a&os_versions=9.1&page=0&pagesize=20&screensize=1242&track_device_info=iPhone72C1&track_deviceid=2F83AC09-B470-4C43-9DEE-E0FB95B4B438&track_user_id=1457891&v=7", self.category];
    [AFN getdataWithString:url block:^(id block) {
        NSMutableDictionary *dic = block;
        self.topicArr = [NSMutableArray array];
        self.topicArr = [HomePageTableViewModel baseModelByArr:dic[@"data"][@"topic"]];
        
        self.progress.hidden = YES;
        [self.tableView reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
